<template>
	<div>
		<h3>修改密码</h3>
		<el-form :model="passwordChangedForm" status-icon :rules="rules" ref="passwordChangedForm" label-width="100px"
			class="demo-ruleForm">
			<el-form-item label="原密码" prop="originalPassword">
				<el-input type="password" v-model="passwordChangedForm.originalPassword" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="新密码" prop="newPassword">
				<el-input type="password" v-model="passwordChangedForm.newPassword" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('passwordChangedForm')">修改</el-button>
				<el-button @click="resetForm('passwordChangedForm')">重置</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import request from '../../../utils/axios/request';
	export default {
		inject:['reload'],
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入原密码'));
				} else {
					if (this.passwordChangedForm.newPassword !== '') {
						this.$refs.passwordChangedForm.validateField('newPassword');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入新密码'));
				} else if (value == this.passwordChangedForm.originalPassword) {
					callback(new Error('新密码不能与原密码相同'));
				} else {
					callback();
				}
			};
			return {
				passwordChangedForm: {
					originalPassword: '',
					newPassword: ''
				},
				rules: {
					originalPassword: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					newPassword: [{
						validator: validatePass2,
						trigger: 'blur'
					}],
				}
			};
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						alert('submit!');
						request({
							method: 'post',
							url: '/user/updatePwd',
							params: {
								uid: 123,
								oPwd: this.passwordChangedForm.originalPassword,
								nPwd: this.passwordChangedForm.newPassword
							}
						}).then((res)=>{
							console.log(res)
							if (res.data.code==10) {
								alert("密码修改成功")
								this.reload();
							}
							else if (res.data.code==101) {
								alert("密码修改失败")
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style scoped>
</style>